package com.shundr.auth.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.shundr.auth.R;
import com.shundr.auth.base.BaseListActivity;
import com.shundr.auth.common.adapter.TabPagerAdapter;

/**
 * 认证货主个人 AuthOwnnerManagerActivity
 * 
 * @author larry 2015-4-19 下午2:18:36
 * @version 1.0.0
 *
 */
public class AuthOwnnerManagerActivity extends BaseListActivity implements
		ActionBar.TabListener, OnPageChangeListener {

	private ViewPager mViewPager;// ViewPager对象的引用
	private String[] mTabTitles = { "未认证", "全部", "认证通过", "认证失败" };// 顶部Tab的title
	private List<Fragment> mFragmentList;// 装载Fragment的容器，我们的每一个界面都是一个Fragment

	private AuthOwnnerListFragment fragment1;
	private AuthOwnnerListFragment fragment2;
	private AuthOwnnerListFragment fragment3;
	private AuthOwnnerListFragment fragment4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		// mActionBar.getCustomView().setVisibility(View.GONE);
		mFragmentList = new ArrayList<Fragment>();
		setContentView(R.layout.activity_main_new);
		initView();
	}

	// 初始化
	private void initView() {
		mViewPager = (ViewPager) findViewById(R.id.viewPager);

		// 为ActionBar添加Tab并设置TabListener
		for (int i = 0; i < mTabTitles.length; i++) {
			ActionBar.Tab tab = mActionBar.newTab();
			tab.setText(mTabTitles[i]);
			tab.setTabListener(this);
			mActionBar.addTab(tab, i);
		}
		fragment1 = new AuthOwnnerListFragment(0);// 全部
		fragment2 = new AuthOwnnerListFragment(1);// 未认证
		fragment3 = new AuthOwnnerListFragment(2);// 认证通过
		fragment4 = new AuthOwnnerListFragment(3);// 认证失败
		mFragmentList.add(fragment2);
		mFragmentList.add(fragment1);
		mFragmentList.add(fragment3);
		mFragmentList.add(fragment4);

		// 设置Adapter
		TabPagerAdapter adapter = new TabPagerAdapter(
				getSupportFragmentManager(), mFragmentList);
		mViewPager.setAdapter(adapter);
		// 设置监听
		mViewPager.setOnPageChangeListener(this);
		mViewPager.setOffscreenPageLimit(3);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int arg0) {
		mActionBar.setSelectedNavigationItem(arg0);

	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		mViewPager.setCurrentItem(tab.getPosition());

	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		switch (resultCode) {
		case 500:
			fragment1.onRefresh();
			fragment2.onRefresh();
			fragment3.onRefresh();
			fragment4.onRefresh();
			break;

		default:
			break;
		}
	}

}
